package com.venutech.gongan.service.impl;

import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.venutech.gongan.dao.EquipmenMapper;
import com.venutech.gongan.dao.JkgwMapper;
import com.venutech.gongan.dto.rpc.common.vo.PoleDto;
import com.venutech.gongan.entity.Equipmen;
import com.venutech.gongan.entity.Jkgw;
import com.venutech.gongan.entity.QRCode;
import com.venutech.gongan.entity.TempUserBindJkgw;
import com.venutech.gongan.service.EquipmenService;
import com.venutech.gongan.service.JkgwService;
import com.venutech.gongan.service.QRCodeService;
import com.venutech.gongan.vo.Equpmenvo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Author liuzhengli
 * @Description //TODO 杆位与设备绑定表
 * @Date  2021/8/6 0006 14:58
 * @Param * @param null:
 * @return
 */
@Service
public class EquipmenServiceImpl implements EquipmenService {

    Logger logger = LoggerFactory.getLogger(EquipmenServiceImpl.class);

    @Resource
    private EquipmenMapper equipmenMapper;

    @Resource
    private JkgwService jkgwService;
    @Resource
    private QRCodeService qrCodeService;

    @Resource
    private EquipmenService equipmenService;

    @Resource
    private JkgwMapper jkgwMapper;

    @Override
    public int insertSbBind(Integer qr_id,Integer gw_id, Integer sb_id) {
        return equipmenMapper.insertSbBind(qr_id,gw_id,sb_id);
    }

    @Override
    public List<Integer> findByGwID(Integer gw_id) {
        return equipmenMapper.findByGwID(gw_id);
    }

    @Override
    public List<Equipmen> getGwIdList() {
        return equipmenMapper.getGwIdList();
    }

    @Override
    public int deleteByGwid(Integer gw_id) {
        return equipmenMapper.deleteByGwid(gw_id);
    }

    /**
     * 根据poleId,equipId参数来解绑指定的设备
     * @param poleId 杆位ID
     * @param equipId 设置ID
     * @return true
     */
    @Override
    public boolean unBindPoleid(Integer poleId, Integer equipId) {

        return equipmenMapper.unBindPoleid(poleId,equipId) > 0 ? true : false;
    }

    /**
     * 在解绑的时候防止重复提交解绑
     * @param poleId
     * @param equipId
     * @return
     */
    @Override
    public boolean findQdsxjByEquipId(Integer poleId, Integer equipId) {
        return equipmenMapper.findQdsxjByEquipId(poleId, equipId) == null ? true:false;
    }


    /**
     * 根据gw_id编号找到绑定对应的标位
     * @param
     * @return
     */
    @Override
    public Jkgw findByJkgwId(int gw_id) {
        return  equipmenMapper.findByJkgwId(gw_id);
    }

    public int addDataTryCatch( Equpmenvo equpmenvo){
        int Bind_peripheral_num =0;
        try {
            Jkgw pole = jkgwService.getById(equpmenvo.getPoleId());
            System.err.println(pole+"--------------pole");
            QRCode qrCode = qrCodeService.getByQrBm(pole.getGw_bm());
            System.err.println(qrCode);
            Integer qrId = 0;
            if(Objects.nonNull(qrCode)) {
                qrId = qrCode.getQr_id();
            }
            System.err.println(qrId+"----------------------qrId");
            if(equpmenvo.getEquipIdList().size()>0){
                for (int i = 0; i <equpmenvo.getEquipIdList().size() ; i++) {
                    //绑定插入操作
                    Bind_peripheral_num = equipmenService.insertSbBind(qrId,equpmenvo.getPoleId(),
                            equpmenvo.getEquipIdList().get(i));
                }
            }
            // 保存当前搜索半径
            System.err.println(equpmenvo.getRadius());
            if(equpmenvo.getRadius() !=null){
                pole.setGw_search_radius(equpmenvo.getRadius());
                int n=jkgwMapper.updateJkgw(pole);
                System.err.println(n);
            }
        }catch (Exception e){
            logger.error("客户端添加数据失败",e);
            return 0;
        }

        return Bind_peripheral_num;
    }


    @Override
    public List<Equipmen> getSbidByGwid(List<Equipmen> gwids) {
        return equipmenMapper.getSbidByGwid(gwids);
    }

    @Override
    public List<Equipmen> getByPoleId(int poleId) {
        return equipmenMapper.getByGwid(poleId);
    }

    public List<Equipmen> getByEquipId(int equipId) {
        return equipmenMapper.getBySbid(equipId);
    }

    /**
     * @Author liuzhengli
     * @Description //TODO根据设应绑定的备id来查找对杆位
     * @Date  2021/8/6 0006 14:58
     * @Param * @param null:
     * @return
     */
    @Override
    public  Equipmen getEquipmentBySbId(Integer sb_id) {

        return equipmenMapper.getEquipmentBySbId(sb_id);
    }

    @Override
    public PoleDto findByPoleId(Integer gw_id) {
        return equipmenMapper.findByPoleId(gw_id);
    }

    /**
     * @Author liuzhengli
     * @Description //TODO  该方法主要是用来判断该设备有没有绑定
     * @Date  2021/8/10 0010 20:14
     * @Param * @param null: 
     * @return 
     */
    @Override
    public boolean deleteEquipmen(Integer poleId) {
        return equipmenMapper.deleteEquipmen(poleId) != null?true:false;
    }
    /**
     * @Author liuzhengli
     * @Description //TODO 根据二维码id和杆位id去绑定表查询是否存在
     * @Date  2021/8/21 0021 21:17
     * @Param * @param null:
     * @return
     */
    @Override
    public Equipmen selectTbEquipmen(Integer qrid, Integer gw_id) {
        return equipmenMapper.getTbEquipmen(qrid,gw_id);
    }

    /**
     * @Author liuzhengli
     * @Description //TODO 根据二维码id和杆位id去绑定表查询是否存在
     * @Date  2021/8/21 0021 21:30
     * @Param * @param null:
     * @return
     */
    @Override
    public int insertTbTempUserBindJkgw(Integer tmp_user_id, String qrIdMD5) {
        return equipmenMapper.insertTbTempUserBindJkgw(tmp_user_id,qrIdMD5);
    }

    @Override
    public TempUserBindJkgw getTempUserBindJkgw(String qrIdMD5) {
        return equipmenMapper.findTempUserBindJkgwByQrIdMd5(qrIdMD5);
    }

    @Override
    public boolean findBygwid(Integer poleId) {
        return !CollectionUtils.isEmpty(equipmenMapper.findByGwidPoleId(poleId));
    }
}
